package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.service.IDestinatonService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinatonService {
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private RegionMapper regionMapper;

    public List<Destination> selectAllDest(int deep) {
        return destinationMapper.selectAlldestByDeep(deep);
    }

    public PageInfo query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Destination> list = destinationMapper.queryForList(queryObject);
        return new PageInfo(list);
    }

    public void changeHotState(Boolean hot, Long id) {
        destinationMapper.changeHotState(hot,id);
    }

    public void updateInfoById(Destination destination) {
        destinationMapper.updateInfoById(destination);
    }

    public List<Destination> getToasts(Long parentId) {
        List<Destination> list = new ArrayList<>();
        creatToasts(parentId,list);
        Collections.reverse(list);
        return list;
    }

    public List<Destination> queryDestByRegionId(Long regionId) {
        if(regionId == -1){
            return destinationMapper.selectDestByRegionId(1L);
        }else{
            Region region = regionMapper.selectByPrimaryKey(regionId);
            Long[] refIds = region.getRefIds();
            return destinationMapper.selectDestByRefids(refIds);
        }

    }

    public boolean isAbroad(Long id) {
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        if (dest == null){return false;}
        if ("中国".equals(dest.getName())){
            return false;
        }
        if (dest.getParent() !=null ){
            if ("中国".equals(dest.getParent().getName())){
                return false;
            }
        }else {
            return true;
        }
        return isAbroad(dest.getParent().getId());
    }

    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    public Destination getCountry(Long destId) {
        Destination dest = destinationMapper.selectByPrimaryKey(destId);
        if(dest.getParent() == null){
            return dest;
        }
        List<Destination> toasts = this.getToasts(destId);
        if(toasts != null && toasts.size()>0){
            return toasts.get(0);
        }
        return null;
    }

    public Destination getProvince(Long destId) {
        if(this.isAbroad(destId)){
            return null;
        }
        Destination dest = destinationMapper.selectByPrimaryKey(destId);
        if(dest.getDeep() == 1){
            return null;
        }
        if(dest.getDeep() == 2){
            return dest;
        }
        if(dest.getDeep() == 3){
            return dest.getParent();
        }
        return null;
    }

    private void creatToasts(Long parentId, List<Destination> list) {
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if (destination == null){
            return;
        }
        list.add(destination);
        if(destination.getParent() != null){
            creatToasts(destination.getParent().getId(),list);
        }
    }
}
